package com.yhxd.modular.news.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yhxd.modular.news.service.UploadService;
import com.yhxd.modular.system.dao.ConfigDao;
import com.yhxd.modular.system.entity.Config;
import com.yhxd.modular.system.vo.ResultVo;
import io.swagger.annotations.Api;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping(value = "/file")
@Api
public class UploadHandleController extends HttpServlet {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private ConfigDao configDao;

    @RequestMapping(value = "/upload")
    @RequiresPermissions("upload")
    @ResponseBody
    public ResultVo doPost(HttpServletRequest request){
        Map<String, Config> map = configDao.findAll().stream().collect(Collectors.toMap(Config::getConfKey, Function.identity()));
        //获取文件格式
        String[] fileFormats=map.get("file_format").getConfValue().split(",");
        //获取文件大小
        int maxSize=Integer.valueOf(map.get("attachment_key").getConfValue());
        String url = map.get("download_upload").getConfValue();
        try{
            //获取文件格式
            String savePath=map.get("upload_upload").getConfValue();
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            String path =uploadService.uploadFiles(multipartRequest,savePath);
            return ResultVo.success(path);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/download")
    @RequiresPermissions("download")
    @ResponseBody
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String downloadUrl=request.getParameter("path");
        FileInputStream in = new FileInputStream(downloadUrl);
        //创建输出流
        OutputStream out = response.getOutputStream();
        //创建缓冲区
        byte buffer[] = new byte[1024];
        int len = 0;
        //循环将输入流中的内容读取到缓冲区当中
        while((len=in.read(buffer))>0){
            //输出缓冲区的内容到浏览器，实现文件下载
            out.write(buffer, 0, len);
        }
        //关闭文件输入流
        in.close();
        //关闭输出流
        out.close();
        IOUtils.copy(in, out);
    }
}
